using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Plugin.Sns.Scene;
using MicroMsg.Protocol;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace MicroMsg.Plugin
{
	public class ExtentCenter
	{
		private const string TAG = "ExtentCenter";
		public static void initialize()
		{
			EventCenter.registerEventHandler(EventConst.ON_ACCOUNT_LOGIN, new EventHandlerDelegate(ExtentCenter.onAccountLoginProc));
			NetSceneSnsSync.Init();
		}
		private static void onAccountLoginProc(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			PluginMetaStorage.loadMetaFromXML();
			PluginLoader.scanAvailablePluginList();
		}
		public static void uninitialize()
		{
			PluginManager.searchAllPlugin(delegate(PluginBase plug)
			{
				plug.onUnInitialize();
				return true;
			});
		}
		public static void onPluginFlagChanged(uint newFlag, uint oldFlag)
		{
			PluginManager.searchAllPlugin(delegate(PluginBase plug)
			{
				ExtentCenter.syncPluginInstall(plug, newFlag);
				return true;
			});
		}
		public static void syncPluginInstall(PluginBase plug, uint newFlag)
		{
			if (!PluginBase.isValidPlugin(plug))
			{
				return;
			}
			if (plug.mProtocolPluginFlag != (EPluginFlag)0)
			{
				uint num = newFlag & (uint)plug.mProtocolPluginFlag;
				if (num != 0u && plug.isInstalledPlugin())
				{
					ExtentCenter.uninstallPlugin(plug, InstallMode.SyncInstall);
					return;
				}
				if (num == 0u && !plug.isInstalledPlugin())
				{
					ExtentCenter.installPlugin(plug, InstallMode.SyncInstall);
				}
			}
		}
		public static bool registerPlugin(PluginBase plugin)
		{
			if (plugin == null)
			{
				return false;
			}
			if (PluginBase.isValidPlugin(plugin))
			{
				return true;
			}
			Log.smethod_4("ExtentCenter", "register plugin... " + plugin.mName);
			plugin.onRegisterResult(RetConst.MM_OK, 0);
			plugin.mMetaInfo.isRegistered = true;
			PluginMetaStorage.updateMetaInfo(plugin);
			return true;
		}
		public static bool unregisterPlugin(PluginBase plugin)
		{
			if (plugin == null)
			{
				return false;
			}
			if (!PluginBase.isValidPlugin(plugin))
			{
				return true;
			}
			Log.smethod_4("ExtentCenter", "unregister plugin... " + plugin.mName);
			if (plugin.isInstalledPlugin())
			{
				plugin.onUninstalled(InstallMode.DefaultInstall);
				plugin.mMetaInfo.isInstalled = false;
			}
			plugin.onUnRegisterResult(RetConst.MM_OK, 0);
			plugin.mMetaInfo.isRegistered = false;
			PluginMetaStorage.updateMetaInfo(plugin);
			return true;
		}
		public static bool installPlugin(PluginBase plugin, InstallMode mode = InstallMode.UserInstall)
		{
			if (!PluginBase.isValidPlugin(plugin))
			{
				return false;
			}
			if (PluginBase.isInstalledPlugin(plugin))
			{
				return false;
			}
			Log.smethod_4("ExtentCenter", string.Concat(new object[]
			{
				"install plugin... ",
				plugin.mName,
				", install mode =",
				mode
			}));
			plugin.onInstalled(mode);
			plugin.mMetaInfo.isInstalled = true;
			PluginMetaStorage.updateMetaInfo(plugin);
			if (mode == InstallMode.UserInstall && plugin.mProtocolPluginFlag != (EPluginFlag)0)
			{
				Account curAccount = AccountMgr.getCurAccount();
				curAccount.nPluginFlag &= (uint)(~(uint)plugin.mProtocolPluginFlag);
				AccountMgr.updateAccount();
				OpLogMgr.opModUserInfo(2048u, curAccount);
			}
			ExtentCenter.initializePlugin(plugin);
			return true;
		}
		public static bool initializePlugin(PluginBase plugin)
		{
			if (!PluginBase.isInstalledPlugin(plugin))
			{
				return false;
			}
			Log.smethod_4("ExtentCenter", "initialize plugin... " + plugin.mName);
			plugin.onInitialize();
			return true;
		}
		public static bool uninstallPlugin(PluginBase plugin, InstallMode mode = InstallMode.UserInstall)
		{
			if (!PluginBase.isValidPlugin(plugin))
			{
				return false;
			}
			if (!PluginBase.isInstalledPlugin(plugin))
			{
				return false;
			}
			Log.smethod_4("ExtentCenter", string.Concat(new object[]
			{
				"uninstall plugin... ",
				plugin.mName,
				", uninstall mode =",
				mode
			}));
			plugin.onUninstalled(mode);
			plugin.mMetaInfo.isInstalled = false;
			PluginMetaStorage.updateMetaInfo(plugin);
			if (mode == InstallMode.UserInstall && plugin.mProtocolPluginFlag != (EPluginFlag)0)
			{
				Account curAccount = AccountMgr.getCurAccount();
				curAccount.nPluginFlag |= (uint)plugin.mProtocolPluginFlag;
				AccountMgr.updateAccount();
				OpLogMgr.opModUserInfo(2048u, curAccount);
			}
			return true;
		}
		public static object executePlugin(PluginBase plugin, int entryType, object args)
		{
			if (!PluginBase.isInstalledPlugin(plugin))
			{
				return null;
			}
			Log.smethod_4("ExtentCenter", string.Concat(new object[]
			{
				"execute plugin... ",
				plugin.mName,
				", entry type = ",
				entryType
			}));
			return plugin.execute(entryType, args);
		}
		public static void onAppActivated()
		{
			PluginManager.searchAllPlugin(delegate(PluginBase plug)
			{
				plug.onAppActivated();
				return true;
			});
		}
		public static void onAppDeactivated()
		{
			PluginManager.searchAllPlugin(delegate(PluginBase plug)
			{
				plug.onAppDeactivated();
				return true;
			});
		}
		public static List<PluginBase> getListInTab(MainTabIndex index)
		{
			return PluginManager.getListInTab(index);
		}
		public static List<PluginBase> getAllListInTab(MainTabIndex index)
		{
			return PluginManager.getAllListInTab(index);
		}
		public static List<PluginBase> getAllList()
		{
			return PluginManager.getAllList();
		}
		public static List<PluginBase> getInstalledList()
		{
			return PluginManager.getInstalledList();
		}
		public static List<PluginBase> getUninstalledList()
		{
			return PluginManager.getUninstalledList();
		}
		public static PluginBase findPluginByName(string name)
		{
			return PluginManager.findPluginByName(name);
		}
		public static PluginBase findPluginByID(long long_0)
		{
			return PluginManager.findPluginByID(long_0);
		}
		public static PluginBase findPluginByUserName(string name)
		{
			return PluginManager.findPluginByUserName(name);
		}
		public static PluginBase getPluginByChatMsg(ChatMsg chatMsg)
		{
			if (chatMsg == null)
			{
				return null;
			}
			PluginBase pluginBase_0 = null;
			PluginManager.searchOnePlugin(delegate(PluginBase plug)
			{
				if (plug.onParseChatMsg(chatMsg))
				{
					pluginBase_0 = plug;
					return true;
				}
				return false;
			});
			return pluginBase_0;
		}
		public static string getPluginNameByChatMsg(ChatMsg chatMsg)
		{
			PluginBase pluginByChatMsg = ExtentCenter.getPluginByChatMsg(chatMsg);
			if (pluginByChatMsg == null)
			{
				return null;
			}
			return pluginByChatMsg.mName;
		}
		public static PluginBase getPluginByChatTalker(string talker)
		{
			if (string.IsNullOrEmpty(talker))
			{
				return null;
			}
			PluginBase pluginBase_0 = null;
			PluginManager.searchOnePlugin(delegate(PluginBase plug)
			{
				if (plug.onParseChatTalker(talker))
				{
					pluginBase_0 = plug;
					return true;
				}
				return false;
			});
			return pluginBase_0;
		}
		public static string getPluginNameByChatTalker(string talker)
		{
			PluginBase pluginByChatTalker = ExtentCenter.getPluginByChatTalker(talker);
			if (pluginByChatTalker == null)
			{
				return null;
			}
			return pluginByChatTalker.mName;
		}
		public static T AsObject<T>(object srcObject)
		{
			if (srcObject == null)
			{
				return default(T);
			}
			T t = Activator.CreateInstance<T>();
			if (!ExtentCenter.AsObject(srcObject, t))
			{
				return default(T);
			}
			return t;
		}
		public static bool AsObject(object srcObject, object dstObject)
		{
			if (srcObject == null || dstObject == null)
			{
				return false;
			}
			Type type = srcObject.GetType();
			if (type == null)
			{
				return false;
			}
			MemberInfo[] members = dstObject.GetType().GetMembers();
			MemberInfo[] array = members;
			for (int i = 0; i < array.Length; i++)
			{
				MemberInfo memberInfo = array[i];
				if (memberInfo.get_MemberType() == 4)
				{
					FieldInfo fieldInfo = memberInfo as FieldInfo;
					if (fieldInfo != null)
					{
						FieldInfo field = type.GetField(fieldInfo.get_Name());
						if (field != null)
						{
							object value = field.GetValue(srcObject);
							fieldInfo.SetValue(dstObject, value);
						}
					}
				}
			}
			return true;
		}
	}
}
